package com.spider.servlet;


import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.spider.controller.PersonControll;

public class HttpClientTest {
	private static Logger log = Logger.getLogger(PersonControll.class);
	public static void main(String args[]) {

        try {

			HttpClient httpclient = new DefaultHttpClient();
                        //register https protocol in httpclient's scheme registry
			/* 建立HTTP Post连线 */
			HttpGet httpPost = new HttpGet("http://completion.amazon.co.uk/search/complete?method=completion&mkt=4&client=amazon-search-ui&x=String&search-alias=digital-text&q=sta&qs=&cf=1&noCacheIE=1433745547165&fb=1&sc=1&");
			httpPost.setHeader("Accept","application/javascript, */*;q=0.8");
			httpPost.setHeader("Referer","http://www.amazon.de/dp/B00KAKPZYG/ref=br_imp_ara-1/275-1217088-6758352?_encoding=UTF8&nav_sdd=aps%2Fref%3Dgw_aftv_187642527_1&pf_rd_m=A3JWKAKR8XB7XF&pf_rd_s=desktop-hero-kindle-A&pf_rd_r=1WQ1BC28XD70Z3T5C8GH&pf_rd_t=36701&pf_rd_p=633168907&pf_rd_i=desktop");
			httpPost.setHeader("Accept-Language","zh-CN");
			httpPost.setHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
			httpPost.setHeader("Accept-Encoding","gzip, deflate");
			httpPost.setHeader("Host","completion.amazon.co.uk");
			httpPost.setHeader("Connection","Keep-Alive");
			log.info("REQUEST:" + httpPost.getURI());
			HttpResponse httpResponse = httpclient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String str=EntityUtils.toString(httpResponse.getEntity());
				str=str.substring(13,str.length()-11);
				log.info(str);
			} else {
				log.info("doGet Error Response: " + httpResponse.getStatusLine().toString());
				
			}
		}  catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();

		}
	}
}


